package com.app.view.component.room.seat 
{
	import flash.text.TextField;
	import flash.geom.Point;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import com.greensock.TweenLite;
	
	import com.utils.Utils;
	
	import com.peachcastle.core.Display;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class PaijiuItem extends Display 
	{
		public static const TYPES:Array = [
											{'4221': '至尊寶' }, 
											{ '6666': '天牌' }, { '1111': '地牌' }, 
											{ '4444': '人牌' }, { '3131': '和牌' }, { '5555': '梅牌' },
											{'222222': '長牌' }, { '2222': '板凳' }, { '6565': '斧頭' },
											{'6464': '紅頭' }, { '6161': '麼六' }, { '5151': '麼五' },
											
											{'6354': '雜九' }, { '6253': '雜八' }, { '5243': '雜七' },
											{'4132': '雜五' }, 
											
											{ '6663': '天王' }, { '6654': '天王' },
											{'6311': '地王' }, { '5411': '地王' }, 
											{ '6662': '天槓' }, { '6653': '天槓' }, 
											{ '6211': '地槓' }, { '5311': '地槓' },
											
											{'6652': '天高九' }, { '6111': '地高九' }
										  ];
		private var _paijius:Array =  null;
		
		private var _b_pais:Array = [];
		
		private var _firstGroup:Array = [];
		private var _secondGroup:Array = [];
		
		private var _mcPaiTypeTip:MovieClip = null;
		private var _txt_paiTypeTip:TextField = null;
		private var _tipFirstPoint:Point = null;

		public function PaijiuItem(ui:MovieClip = null) 
		{
			super(ui);
		}
		
		override protected function initialization():void 
		{
			super.initialization();

			_paijius = [];
			for (var i:int = 1; i <= 4; i++)
			{
				var item:Paijiu = new Paijiu(assest["pai" + i]);
				_paijius[i] = item;
				item = null;
			}
			
			for (var j:int = 1; j <= 2; j++ )
			{
				_b_pais[j] = assest["b_pai" + j];
				(assest["b_pai" + j] as MovieClip).visible = false;
			}
			
			_mcPaiTypeTip = assest['mc_type_tip'];
			_tipFirstPoint = new Point(_mcPaiTypeTip.x, _mcPaiTypeTip.y);
			_txt_paiTypeTip = _mcPaiTypeTip['txt_tip'];
			_txt_paiTypeTip.text = '';
			setTypeTip(false);
		}
		
		override protected function configEventListeners():void 
		{
			super.configEventListeners();
			
			for (var i:int = 1; i <= 4; i++)
			{
				Paijiu(_paijius[i]).addEventListener(MouseEvent.CLICK, tipPaijiuType);
			}
		}
		
		private function tipPaijiuType(e:MouseEvent):void
		{
			dispatchEvent(e);
			
			setTypeTip(true);
			
			_firstGroup = [];
			_secondGroup = [];
			for (var i:int = 1; i <= 4; i++)
			{
				if (Paijiu(_paijius[i]).position == 1)
				{
					_firstGroup.push(Paijiu(_paijius[i]).paiNumber);
				}else
				{
					_secondGroup.push(Paijiu(_paijius[i]).paiNumber);
				}
			}
			
			if (_firstGroup.length > 2 || _firstGroup.length <= 1 || _secondGroup.length > 2 || _secondGroup.length <= 1)
			{
				_txt_paiTypeTip.text = "請選擇2張牌.";
			}else if (_firstGroup.length == 2 && _secondGroup.length == 2)
			{
				changeTypeTip(_firstGroup, _secondGroup);
			}
		}
		
		/**
		 * 发牌
		 */
		public function sendCard(mc:MovieClip, seatid:int, cardIndex:int, paijiusData:String, isSelf:Boolean, callback:Function, paisBg:Array, startPositionMc:MovieClip):void			
		{
			var initP:Point = new Point(mc.x, mc.y);
			TweenLite.to(mc, .5, { x: (paisBg[seatid][cardIndex] as MovieClip).x - startPositionMc.x, y: (paisBg[seatid][cardIndex] as MovieClip).y - startPositionMc.y, onComplete:moveCompleteHandler ,onCompleteParams:[seatid, cardIndex, isSelf, paijiusData, mc, initP, callback, paisBg]} );	
		}	
		
		/**
		 * 移动完成
		 */
		private function moveCompleteHandler(seatid:int, cardIndex:int, isSelf:Boolean, data:String, mc:MovieClip, initP:Point, callback:Function, paisBg:Array):void
		{			
			mc.visible = false;
			mc.x = initP.x;
			mc.y = initP.y;
			showValue(seatid, cardIndex, isSelf, data, callback, paisBg);
		}
		
		private var _sdata:Array = null;
		public function showValue(seatid:int, cardIndex:int, isSelf:Boolean, data:String, callback:Function, paisBg:Array):void
		{
			_sdata = data.split(",");
			setValues(seatid, cardIndex, _sdata, isSelf, paisBg);
			
			callback();
		}
		
		private function setValues(seatid:int, cardIndex:int, data:Array, isSelf:Boolean, paisBg:Array):void
		{
			var count:int = 0;
			if (cardIndex == 1)
			{
				count = 2;
			}else 
			{
				count = 4;
			}

			showValues(cardIndex, count, data, isSelf, seatid, paisBg);
		}
		
		public function showValues(cardIndex:int, count:int, data:Array, isSelf:Boolean, seatid:int = 0, paisBg:Array = null):void
		{
			for (var j:int = cardIndex; j <= count; j++)
			{
				if (data[j - 1] != null && data[j - 1] != '')
				{
					if (isSelf)
					{
						Paijiu(_paijius[j]).show(data[j - 1], true);
					}else
					{
						if(paisBg) paisBg[seatid][j].visible = true;
					}
				}
			}
		}
		
		public function showResult(data:Array, isShowBig:Boolean):void
		{
			setTypeTip(true);
			
			_firstGroup = [data[0], data[1]];
			_secondGroup = [data[2], data[3]];
			
			changeTypeTip(_firstGroup, _secondGroup);
			
			for (var j:int = 1; j <= 4; j++)
			{
				if (data[j - 1] != null && data[j - 1] != '')
				{
					Paijiu(_paijius[j]).show(data[j - 1]);
					if (j <= 2)
					{
						var x:Number = _b_pais[j].x;
						var y:Number = _b_pais[j].y;
						
						if (!isShowBig)
						{
							x = x - 5;
							if (j == 2)
							{
								y = y - 10;
							}
						}
						Paijiu(_paijius[j]).rotatePosition(x, y, _b_pais[j].rotation);
					}
					
					if (!isShowBig) Paijiu(_paijius[j]).setPaijiuScale(.7, j);
				}
			}
			
			if (!isShowBig) _mcPaiTypeTip.y = _mcPaiTypeTip.y - 20;
		}
		
		public function hidePaijius():void
		{
			for (var i:int = 1; i <= 4; i++)
			{
				Paijiu(_paijius[i]).setPaijiuScale(1);
				Paijiu(_paijius[i]).resetPosition();
				Paijiu(_paijius[i]).hideAssest();
			}
			
			resetTypeTip();
		}
		
		public function getPaijiuPosition():String
		{
			_firstGroup = [];
			_secondGroup = [];
			for (var i:int = 1; i <= 4; i++)
			{
				if (Paijiu(_paijius[i]).position == 1)
				{
					_firstGroup.push(Paijiu(_paijius[i]).paiNumber);
				}else
				{
					_secondGroup.push(Paijiu(_paijius[i]).paiNumber);
				}
			}

			if (_secondGroup.length == 4 && _firstGroup.length == 0)
			{
				var temp:Array = Utils.clone(_secondGroup) as Array;
				
				_secondGroup = [];
				
				_firstGroup.push(temp[0]);
				_firstGroup.push(temp[1]);
				
				_secondGroup.push(temp[2]);
				_secondGroup.push(temp[3]);
			}

			if (_firstGroup.length != 2 && _secondGroup.length != 2)
			{
				return '';
			}
			
			var a:Array = judgeType(_firstGroup);
			var b:Array = judgeType(_secondGroup);
			var pkR:int = PaijiuType.pk(a, b);
			if (pkR < 0)
			{
				return _firstGroup.concat(_secondGroup).join(",");
			}else
			{
				return _secondGroup.concat(_firstGroup).join(",");
			}
		}
		
		private function changeTypeTip(first:Array, second:Array):void
		{
			var a:Array = judgeType(first);
			var b:Array = judgeType(second);
			var pkR:int = PaijiuType.pk(a, b);
			if (pkR == 1)
			{
				_txt_paiTypeTip.text = b[1] + " / " + a[1];
			}else
			{
				_txt_paiTypeTip.text = a[1] + " / " + b[1];
			}
		}
		
		private function judgeType(pais:Array):Array
		{
			return PaijiuType.judgeType(pais);
		}
		
		public function changePaijiuStatus(isActive:Boolean):void
		{
			for (var i:int = 1; i <= 4; i++)
			{
				Paijiu(_paijius[i]).changePaijiuStatus(isActive);
			}
		}
		
		public function setTypeTip(b:Boolean = false):void
		{
			if (_mcPaiTypeTip.visible != b) _mcPaiTypeTip.visible = b;
		}
		
		public function resetTypeTip():void
		{
			_mcPaiTypeTip.x = _tipFirstPoint.x;
			_mcPaiTypeTip.y = _tipFirstPoint.y;
			_mcPaiTypeTip.visible = false;
		}
	}

}